package cn.edu.scau.cmi.zhuangjunsheng.comprehensiveExperiment;

import cn.edu.scau.cmi.zhuangjunsheng.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.zhuangjunsheng.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.zhuangjunsheng.composite.consistent.ConsistentLeaf;
import cn.edu.scau.cmi.zhuangjunsheng.domain.Meat;
import cn.edu.scau.cmi.zhuangjunsheng.factory.BeefMeatFactory;
import cn.edu.scau.cmi.zhuangjunsheng.factory.ChickenMeatFactory;
import cn.edu.scau.cmi.zhuangjunsheng.factory.MeatFactory;
import cn.edu.scau.cmi.zhuangjunsheng.factory.PorkMeatFactory;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class ConsistentCompositeUI {
	
	@FXML
	private Button add;
	@FXML
	private Button delete;
	@FXML
	private Button check;
	@FXML
	private TextField tf1;
	@FXML
	private TextField tf2;
	@FXML
	private TextField tf3;
	@FXML
	private TextField tf4;
	@FXML
	private TextField tf5;
	@FXML
	private TextArea ta;
	ConsistentComponent organization;
	ConsistentComponent team1;
	ConsistentComponent team2;
	ConsistentComponent team3;
	public ConsistentCompositeUI() {
		organization = new ConsistentComposite("学校");
		organization.parent = new ConsistentComposite("无");
		team1 = new ConsistentComposite("数学与信息学院");
		team2 = new ConsistentComposite("经管学院");
		team3 = new ConsistentComposite("电子工程学院");
		organization.children.clear();
		organization.addChild(team1);
		organization.addChild(team2);
		organization.addChild(team3);
		team1.parent = team2.parent = team3.parent = organization;
	}
	
	
	public void onAdd(ActionEvent av) throws Exception {
		ta.clear();
		String type = tf1.getText();
		String tarName = tf2.getText();
		String newName = tf5.getText();
		ConsistentLeaf newPerson = null;  ConsistentComposite newTeam = null;
		if(type.equals("个人")) {
			newPerson = new ConsistentLeaf(newName);
			newPerson.children.clear();
		}
		else {
			newTeam = new ConsistentComposite(newName);
			newTeam.children.clear();
		}
		ConsistentComponent target = organization.get(tarName);
		if(target==null) ta.appendText("该节点无法加入\n");
		else {
			if(type.equals("个人")) {
				newPerson.parent = target;
				target.addChild(newPerson);
			}
			else {
				newTeam.parent = target;
				target.addChild(newTeam);
			}
			ta.appendText("添加成功\n");
		}
	} 
	
	public void onDelete(ActionEvent av) throws Exception {
		ta.clear();
		String type = tf3.getText();
		String tarName = tf4.getText();
		ConsistentComponent target = organization.get(tarName);
		if(target==null) {
			ta.appendText("该节点不存在");
		}
		else if(target.getName().equals("学校")) ta.appendText("该节点无法删除\n");
		else {
			target.parent.deleteChild(target);
			ta.appendText("删除成功\n");
		}
	} 
	
	public void onCheck(ActionEvent av) throws Exception {
		
		ta.clear();
		ta.appendText(organization.print1(4));
	}
	
}
